package jp.co.dailyReport.customer.sql;

import java.util.Vector;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.apache.log4j.Logger;
import jp.co.dailyReport.Executer;
import jp.co.dailyReport.Utilities;
import jp.co.dailyReport.customer.Customer;

/**
 * 取引先情報を取得するクラスです。
 */
public class CustomersLoadExecuter implements Executer {

	private static Logger log = Logger.getLogger(CustomersLoadExecuter.class);

	/**
	 * 取引先情報を取得します。
	 * 
	 * @param conn コネクションオブジェクト
	 * @return 取引先オブジェクトの配列
	 * @throws SQLException
	 */
	public Object[] execute(Connection conn) throws SQLException {

		ResultSet rset = null;
		PreparedStatement pstmt = null;
		String sql = "select * from customer where delFlg=0 order by customerId";
		log.debug("sql: " + sql);
		Vector answer = new Vector();
		try {
			pstmt = conn.prepareStatement(sql);
			Customer customer;
			for(rset = pstmt.executeQuery(); rset.next(); answer.add(customer)) {
				customer = new Customer();
				customer.setCustomerId(rset.getInt("customerId"));
				customer.setName(Utilities.convUniqueCharacter(rset.getString("name")));
				customer.setDelFlg(Utilities.decimalToBoolean(rset.getInt("delFlg")));
				customer.setUpDay(Utilities.getCalendar(rset.getDate("upDay")));
			}
		} finally {
			if(rset != null)
			 rset.close();
			if(pstmt != null)
			 pstmt.close();
		}
		return (Customer[])answer.toArray(new Customer[0]);
	}
}
